
#include "MathUtils.h"

LOCO_BEGIN

//================================================================================
Matrix4 rotation_matrix_from_euler(Vector3 const& euler)
{
	float yaw = euler.x;
	float pitch = euler.y;
	float roll = euler.z;

	Matrix4 result;
	
	const float	sr = sin(roll * PI / 180.f);
	const float	sp = sin(pitch * PI / 180.f);
	const float	sy = sin(yaw * PI / 180.f);
	const float	cr = cos(roll * PI / 180.f);
	const float	cp = cos(pitch * PI / 180.f);
	const float	cy = cos(yaw * PI / 180.f);

	result.m[0][0] = cp * cy; 
	result.m[1][0] = cp * sy;
	result.m[2][0] = sp;
	result.m[3][0] = 0.0f;

	result.m[0][1] = sr * sp * cy - cr * sy;
	result.m[1][1] = sr * sp * sy + cr * cy;
	result.m[2][1] = -sr * cp;
	result.m[3][1] = 0.0f;

	result.m[0][2] = -(cr * sp * cy + sr * sy);
	result.m[1][2] = cy * sr - cr * sp * sy;
	result.m[2][2] = cr * cp;
	result.m[3][2] = 0.0f;

	result.m[0][3] = 0.0f;
	result.m[1][3] = 0.0f;
	result.m[2][3] = 0.0f;
	result.m[3][3] = 1.0f;

	return result;
}

//================================================================================
Matrix4 perspective_projection(float fov_y, float aspect_ratio, float near_z, float far_z)
{
	return Matrix4(	tan(0.5f*fov_y) / aspect_ratio,		0.0f,						0.0f,									0.0f,
					0.0f,								1.0 / tan(0.5f*fov_y),		0.0f,									0.0f,
					0.0f,								0.0f,						(far_z + near_z) / (near_z - far_z),	2.0f * near_z * far_z  / (near_z - far_z),
					0.0f,								0.0f,						-1.0f,									0.0f);
}


LOCO_END